
from GameLobbyBase import GameLobbyBase
from Cube.command.QCommand.EventDefine import event_QCMD_CLIENTSETUP
# event
from Shell.fsm.EventDefine import event_NETWORKGAME_GAMELOBBY_CLIENTCREATE
# event of fsm
from Shell.fsm.EventDefine import event_NETWORKGAME_BACKTOMAINMENU
# uiString
from Shell.data.UIString import uiString
# gui
from direct.gui.DirectGui import OkDialog
# cmd
from Cube.command.QCommand import QCommand, QCmdMemberLeave
# test: map loader
from Cube.map import SingleMapHeadLoad, MapMgrGet
import sys, os

class GameLobbyClient(GameLobbyBase):
    def __init__(self):
        GameLobbyBase.__init__(self)
        # self._MapStructureInit() # init map structure at ClientSetupHandle

        # event handle
        self.acceptOnce(event_QCMD_CLIENTSETUP, self.ClientSetupHandle)

    # client only
    def ClientSetupHandle(self, mapNamem, checksum, memberID, syncData):
        # TODO...
        # map loading from checksum
        bSuccess = self._MapLoading(checksum)
        self._MapStructureInit(bSuccess)  # init map structure
        if bSuccess:
            # notify GameLobbyFrame to create view and controller
            messenger.send(event_NETWORKGAME_GAMELOBBY_CLIENTCREATE)
            self._SyncDataSet(syncData) # syncDsata process
            self._MyMemberIDSet(memberID)
            self._MemberJoinCmdSend(memberID)  # send member join cmd
        else:
            # map loading fail - leave game lobby
            QCommand.MyMemberID = memberID # set MyMemberID in QCommand klass
            self._MemberJoinCmdSend(memberID)  # send member join cmd
            QCmdMemberLeave().Send(memberID)   # send member leave cmd

            msgString = "%s'%s'"%(uiString["Can not find map - "], mapNamem)
            self.MessageDisplay(msgString, self.DestroyMsgBoxAndLeaveGameLobby)

    def _SyncDataSet(self, syncData):
        # memberList
        for memberData in syncData["memberList"]:
            self.MemberJoinHandle(*memberData)
        # random seed
        MapMgrGet().CurrentMapHeader.RandomSeedSet(syncData["randomSeed"])

    # internal function
    def _MapLoading(self, checksum):
        # ----
        # add path into sys.path
        mapPath = "%s\\Shell\\ghost\\SampleMap"%(os.getcwd())
        sys.path.append(mapPath)
        from SampleMap import SampleMap
        # ----
        mapHeaderKlass = SampleMap # test
        SingleMapHeadLoad(SampleMap)
        self._CmdDispatchPeriodsReset()
        bSuccess = True
        return bSuccess

    def _CmdDispatchPeriodsReset(self):
        CmdDispatchPeriods = MapMgrGet().CmdSyncPeriodGet()
        #self.CmdInputPipe.CmdDispatchPeriodsReset(CmdDispatchPeriods)

    def DestroyMsgBoxAndLeaveGameLobby(self, value):
        self.MessageDisplayDialogDestroy()
        messenger.send(event_NETWORKGAME_BACKTOMAINMENU)

# messages display --------------------------------------------------
    def MessageDisplay(self, uiString, fn):
        self.MsgDisplayDialog = OkDialog(text = uiString, command = fn)

    def MessageDisplayDialogDestroy(self):
        self.MsgDisplayDialog.destroy()

    # override
    def IPAddressGet(self):
        ipAddress = self.LoginInfo["HostIP"]
        return ipAddress